package com.example.sources.util;

import com.example.sources.annotation.MyController;
import com.example.sources.annotation.MyRequestMapping;
import com.example.sources.annotation.MyResponseString;
import com.example.sources.config.SystemConfig;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhu on 2016/5/9.
 */
public class AnnotationUtil {
    private static AnnotationUtil annotationUtil = null;
    public static AnnotationUtil getinstall(){
        if (annotationUtil == null){
            annotationUtil = new AnnotationUtil();
        }
        return annotationUtil;
    }
        private  List<MapBean> mapBeans;

    private  List<Class> getClassList(String dir, String srcPath) throws ClassNotFoundException {
        List<Class> classList = new ArrayList<Class>();
        String newDir = dir.replace(".","/");
        String abSolutePath = srcPath + newDir;
        String ss = eliminatePrefix(abSolutePath);
        abSolutePath = ss.replace("/",File.separator);
        File dirFile = new File(abSolutePath);
        File[] files = dirFile.listFiles();
        Class<?> cla = null;
        for (File file:files){
            if (file.isDirectory()){
                String child = dir + "."+file.getName();
                getClassList(child, getSrcPath());
            }else {
                cla = Class.forName(dir+"."+file.getName().split("\\.")[0]);
                classList.add(cla);
            }
        }

        return classList;
    }

    private   String getSrcPath(){
        String path="";
        try {
            path =Thread.currentThread().getContextClassLoader().getResource("")+"";  //获取src路径

        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    private  void setRequestMapping(List<Class> list) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
        for (Class<?> cla:list){
            if (cla.getAnnotation(MyController.class)!=null){
                Method[] methods = cla.getMethods();
                for (Method method : methods){
                    if (method.getAnnotation(MyRequestMapping.class)!=null){
                        MyRequestMapping annotation = method.getAnnotation(MyRequestMapping.class);
                        boolean isPathVariable = false;
                        if (annotation.path().contains("{")){
                            isPathVariable = true;
                        }
                        if (method.getAnnotation(MyResponseString.class) != null){

                            mapBeans.add(new MapBean(annotation.path(),annotation.method()[0].toString(),SystemConfig.ResponseString,cla.getName(),method.getName(),isPathVariable));
                        }else {
                            mapBeans.add(new MapBean(annotation.path(),annotation.method()[0].toString(),SystemConfig.ResponsePage,cla.getName(),method.getName(),isPathVariable));
                        }
                    }
                }
            }

        }
    }

    public List<MapBean> getRequestMapping(){
        mapBeans = new ArrayList<MapBean>();
        List<Class> list = null;
        try {
            list = getClassList(PropertiesUtil.getProperties("controllerPath"), getSrcPath());
            setRequestMapping(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mapBeans;
    }

    private String eliminatePrefix(String filepath){
        String[] ss = filepath.split("/");
        String[] strings =  filepath.split(ss[0]+'/');
        return  strings[1];
    }

}
